﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prisa.Entities;
using System.Data;
using System.Data.Objects;

using Microsoft.Practices.Unity;

namespace Prisa.DataAccess
{
    public class CompraDAO:MasterDataAccess,ICompraDAO
    {
        [Dependency]
        public IDetalleCompraDAO detalleComDAO { get; set; }

        [Dependency]
        public IProductoDAO productoDAO { get; set; }

        [Dependency]
        public ICategoriaDAO categoriaDAO { get; set; }


        public List<COMPRA> GetAllFromCompra()
        {
            var hoy = DateTime.Today.Date;
            var query = from c in Context.COMPRAs.Include("PROVEEDOR")
                        orderby c.Fecha descending
                        //where c.Fecha == hoy
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }

        public List<COMPRA> GetFromCompraByProveedor(string descripcion)
        {
            if (descripcion != "")
            {
                var query = from c in Context.COMPRAs.Include("PROVEEDOR")
                            where c.PROVEEDOR.RazonSocial.ToUpper().Contains(descripcion.ToUpper()) || c.PROVEEDOR.Ruc.ToUpper().Contains(descripcion.ToUpper())
                            orderby c.Fecha descending
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);
                
                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.COMPRAs.Include("PROVEEDOR")
                             orderby c.Fecha descending
                             select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query1);

                return query1.ToList();
            }
        }

        public List<COMPRA> GetFromCompraByNroGuia(string descripcion)
        {
            if (descripcion != "")
            {
                var query = from c in Context.COMPRAs.Include("PROVEEDOR")
                            where c.NroGuia.ToUpper().Contains(descripcion.ToUpper()) 
                            orderby c.Fecha descending
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);


                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.COMPRAs.Include("PROVEEDOR")
                             orderby c.Fecha descending
                             select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query1);


                return query1.ToList();
            }
        }

        public COMPRA GetFromCompraById(int id)
        {
            var query = from c in Context.COMPRAs.Include("DETALLECOMPRAs.PRODUCTO")
                        where c.Id.Equals(id)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }

        public void AddCompra(COMPRA compra)
        {
            foreach (var item in compra.DETALLECOMPRAs)
            {
                Context.Entry(item.PRODUCTO).State = EntityState.Unchanged;
                Context.Entry(item).State = EntityState.Added;
            }

            Context.Entry(compra.PROVEEDOR).State = EntityState.Unchanged;
            Context.COMPRAs.Add(compra);
            Context.SaveChanges();

        }

        public void UpdateCompra(COMPRA compra)
        {           
            var estadosDetalle = Context.ChangeTracker.Entries<DETALLECOMPRA>();
            
            Context.Entry(compra.PROVEEDOR).State = EntityState.Unchanged;
            Context.Entry(compra).State = EntityState.Modified;

            foreach (var item in compra.DETALLECOMPRAs)
            {
                item.PRODUCTO = null;

                Context.Entry(item).State = item.Id == Int32.Parse("0") ? System.Data.EntityState.Added : System.Data.EntityState.Modified;
                item.ProductoId = item.ProductoId;
            }
            
            Context.SaveChanges();
            
        }


        public bool GetAllComprasByProveedorId(int idProveedor)
        {
            var query = from c in Context.COMPRAs.Include("PROVEEDOR")
                        where c.ProveedorId.Equals(idProveedor)
                        select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }

        public List<COMPRA> GetFromComprabyFechasAndProveedor(DateTime? fec1, DateTime? fec2, string proveedor)
        {
            if (proveedor != "")
            {
                var query = from h in Context.COMPRAs.Include("PROVEEDOR")
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);


                if (fec1 != null && fec2 != null)//ambos
                {
                    query = from h in query
                            where h.Fecha >= fec1.Value && h.Fecha <= fec2.Value &&
                                    (h.PROVEEDOR.RazonSocial.ToUpper().Contains(proveedor.ToUpper()) || 
                                    h.PROVEEDOR.Ruc.ToUpper().Contains(proveedor.ToUpper()) )
                            select h;
                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.Fecha == fec1.Value &&
                            (h.PROVEEDOR.RazonSocial.ToUpper().Contains(proveedor.ToUpper()) ||
                            h.PROVEEDOR.Ruc.ToUpper().Contains(proveedor.ToUpper()) )
                            select h;
                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                else //retornar todos
                {
                    query = from h in query
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.ToList();
            }
            else
            {
                var query = from h in Context.COMPRAs.Include("PROVEEDOR")
                            select h;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);


                if (fec1 != null && fec2 != null)//solo fecha
                {
                    query = from h in query
                            where h.Fecha >= fec1.Value && h.Fecha <= fec2.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.Fecha == fec1.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.ToList();
            }
        }

        public List<COMPRA> GetFromComprabyFechasAndNroGuia(DateTime? fec1, DateTime? fec2, string nroGuia)
        {
            if (nroGuia != "")
            {
                var query = from h in Context.COMPRAs.Include("PROVEEDOR")
                            select h;
                ObjectContext.Refresh(RefreshMode.StoreWins, query);


                if (fec1 != null && fec2 != null)//ambos
                {
                    query = from h in query
                            where h.Fecha >= fec1.Value && h.Fecha <= fec2.Value &&
                                    h.NroGuia.ToUpper().Contains(nroGuia.ToUpper()) 
                            select h;
                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.Fecha == fec1.Value &&
                                   h.NroGuia.ToUpper().Contains(nroGuia.ToUpper())
                            select h;
                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                else //retornar todos
                {
                    query = from h in query
                            select h;
                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.ToList();
            }
            else
            {
                var query = from h in Context.COMPRAs.Include("PROVEEDOR")
                            select h;
                ObjectContext.Refresh(RefreshMode.StoreWins, query);


                if (fec1 != null && fec2 != null)//solo fecha
                {
                    query = from h in query
                            where h.Fecha >= fec1.Value && h.Fecha <= fec2.Value
                            select h;
                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.Fecha == fec1.Value
                            select h;
                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.ToList();
            }
        }
    }
}
